package com.example.locationservice;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements ServiceConnection {

    public static final int RESULT_CODE = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        methodRequiresTwoPermission();
    }

    //动态权限框架
    @AfterPermissionGranted(RESULT_CODE)
    private void methodRequiresTwoPermission() {
        String[] perms = {Manifest.permission.INTERNET, Manifest.permission.ACCESS_FINE_LOCATION};
        if (EasyPermissions.hasPermissions(MainActivity.this, perms)) {
//            final Intent intentStart = new Intent(MainActivity.this, LocationServer.class);
//            startService(intentStart);
            Log.e("sss","绑定服务");
            bindService(new Intent(this, LocationServer.class), this, BIND_AUTO_CREATE);
        } else {

            EasyPermissions.requestPermissions(MainActivity.this, "请求获取您的位置信息和本地读写权限",
                    RESULT_CODE, perms);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        Log.e("ss","绑定回调");
        LocationServer.Binder binder = (LocationServer.Binder) iBinder;
        LocationServer myService = binder.getService();
        myService.setCallback(new LocationServer.Callback() {
            @Override
            public void onDataChange(Location location) {
                Message msg = new Message();
                msg.obj = location;
                handler.sendMessage(msg);
                String strcontent=location.getLatitude()+","+location.getLongitude()+","+location.getAccuracy();
                // 每次写入时，都换行写
                String strContent = strcontent + "\r\n";
                Log.e("定位结果",strContent);
                Toast.makeText(MainActivity.this,strContent,Toast.LENGTH_LONG).show();

            }
        });
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }


    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_LONG);
        }
    };
}